/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年6月5日
 */
package com.massyframework.beanskin.sample.domain;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.massyframework.beanskin.sample.ToDoItem;
import com.massyframework.beanskin.sample.ToDoItemRepository;
import com.massyframework.beanskin.sample.ToDoItemStore;

/**
 * 基于内存的待办事务资源库
 * @author huangkh
 *
 */
public class MemoryToDoItemRepository implements ToDoItemRepository {

	private Map<Long, ToDoItem> itemMap;
	private ToDoItemStore toDoItemStore;
	
	/**
	 * 
	 */
	public MemoryToDoItemRepository() {
	}

	@Override
	public ToDoItem findById(long id) {
		return this.getToDoItemMap().get(id);
	}

	@Override
	public List<ToDoItem> getToDoItems() {
		return new ArrayList<>(this.getToDoItemMap().values());
	}
	
	
	
	@Override
	public List<ToDoItem> getToDoItems(Predicate<ToDoItem> filter) {
		return this.getToDoItemMap().values().stream()
					.filter(filter)
					.collect(Collectors.toList());
	}
	
	

	@Override
	public List<ToDoItem> getToDoItems(Function<Stream<ToDoItem>, Stream<ToDoItem>> action) {
		Stream<ToDoItem> stream = action.apply(this.getToDoItemMap().values().stream());
		return stream.collect(Collectors.toList());
	}

	@Override
	public int size() {
		return this.getToDoItemMap().size();
	}

	/**
	 * 待办事项Map
	 * @return {@link Map}
	 */
	private Map<Long, ToDoItem> getToDoItemMap(){
		if (this.itemMap == null) {
			synchronized (this) {
				if (this.itemMap == null) {
					Map<Long, ToDoItem> tmp = new ConcurrentHashMap<>();
					List<Map<String, Object>> data = this.getToDoItemStore().loadAll();
					for (Map<String, Object> map: data) {
						ToDoItem item = this.toToDoItemDomain(map);
						if (item != null) {
							tmp.put(item.getId(), item);
						}
					}
					
					this.itemMap = tmp;
				}
			}
		}
		
		return this.itemMap;
	}
	
	
	
	protected ToDoItemDomain toToDoItemDomain(Map<String, Object> data) {
		Long id = (Long)data.get("id");
		String title = (String)data.get("title");
		String description = (String)data.get("description");
		
		return ToDoItemDomain.newBuilder()
			.id(id)
			.title(title)
			.description(description)
			.build();
	}

	/**
	 * 待办事项存储
	 * @return {@link ToDoItemStore}
	 */
	public ToDoItemStore getToDoItemStore() {
		return toDoItemStore;
	}

	/**
	 * 设置待办事项存储
	 * @param toDoItemStore {@link ToDoItemStore}
	 */
	@Inject
	public void setToDoItemStore(@Named("toDoItemStore") ToDoItemStore toDoItemStore) {
		this.toDoItemStore = toDoItemStore;
	}

	
}
